#!/usr/bin/env python

import HTMLParser
import htmlentitydefs

class DataConcattingHtmlParser:
    class _internalParser(HTMLParser.HTMLParser):
        def __init__(self, owner):
            HTMLParser.HTMLParser.__init__(self)
            self._data = None
            self.owner = owner
        
        def handle_data(self, data):
            self.append_data(data)
        
        def append_data(self, data):
            if self._data is None:
                self._data = ''
            self._data += data

        def flush_data(self):
            if self._data is not None:
                self.owner.handle_data(self._data)
                self._data = None

        def close(self):
            self.flush_data()
            HTMLParser.HTMLParser.close(self)
            
        def handle_endtag(self, tag):
            self.flush_data()

        def handle_starttag(self, tag, attrs):
            self.flush_data()
            self.owner.handle_starttag(tag, attrs)

        def handle_entityref(self, name):
            self.append_data(htmlentitydefs.entitydefs.get(name, '[' + name + ']'))


    def __init__(self):
        self.myParser = DataConcattingHtmlParser._internalParser(self)

    def feed(self, data):
        self.myParser.feed(data)
    
    def close(self):
        self.myParser.close()

    def handle_starttag(self, tag, attrs):
        pass




